//
//  LXPageController.m
//  灵秀
//
//  Created by 李田 on 17/3/19.
//  Copyright © 2017年 tianyao. All rights reserved.
//

#import "LXPageController.h"
#import "LXDetailsViewController.h"
#import "LXFoodModel.h"
#import "LXFoodDetailModel.h"
#import "LXBuyCarView.h"
@interface LXPageController ()<UIPageViewControllerDelegate, UIPageViewControllerDataSource, CAAnimationDelegate>
// 判断转场是否结束
@property (nonatomic) BOOL isEnd;

@property (nonatomic, weak) LXBuyCarView *buyCarView;
@end

@implementation LXPageController

- (void)viewDidLoad {
    [super viewDidLoad];
    _isEnd = YES;
    
    // 获取通知中心
    NSNotificationCenter *cationCenter = [NSNotificationCenter defaultCenter];
    
    [cationCenter addObserver:self selector:@selector(addBuyCarViewData:) name:kNotificationNamePageVcBuyCarView object:nil];
}

#pragma mark - 4.0详情页购物车实现
- (void)addBuyCarViewData:(NSNotification *)no
{
    NSDictionary *dic = no.userInfo;
    // 1.获取起点
    CGPoint startP = [dic[@"startP"] CGPointValue];
    // 2.获取数据
    LXFoodDetailModel *model = dic[@"model"];
    
    // 判断增加
    if(![_selectArray containsObject:model]){
        
        [_selectArray addObject:model];
    }

    // 3.放动画
    [self addAnimationWithPoint:startP];
}

#pragma mark - 小红点动画实现
- (void)addAnimationWithPoint:(CGPoint)startP
{
    
    UIImageView *redPoint = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"icon_food_count_bg"]];
    
    redPoint.center = startP;
    
    [[UIApplication sharedApplication].keyWindow addSubview:redPoint];
    
    CAKeyframeAnimation *keyAni = [CAKeyframeAnimation animationWithKeyPath:@"position"];
    
    UIBezierPath *path = [UIBezierPath bezierPath];
    
    [path moveToPoint:startP];

    CGPoint endP = CGPointMake(50, kScreenHeight - 46);
    
    CGPoint controlP = CGPointMake(startP.x - 100, startP.y - 100);
    
    [path addQuadCurveToPoint:endP controlPoint:controlP];
    
    keyAni.path = path.CGPath;
    keyAni.duration = .5;
    keyAni.delegate = self;
    [keyAni setValue:redPoint forKey:@"red"];
    
    [redPoint.layer addAnimation:keyAni forKey:@"keyAni"];
}

#pragma mark - 动画结束--删除小红点
- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag
{
    
    UIImageView *red = (UIImageView *)[anim valueForKey:@"red"];
    [red removeFromSuperview];
    
    _buyCarView.buyArrayList = _selectArray;
}

#pragma mark - 销毁通知
- (void)dealloc
{
    
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - 3.0 细节处理
- (void)pageViewController:(UIPageViewController *)pageViewController willTransitionToViewControllers:(NSArray<UIViewController *> *)pendingViewControllers
{
    
    _isEnd = NO;
}

- (void)pageViewController:(UIPageViewController *)pageViewController didFinishAnimating:(BOOL)finished previousViewControllers:(NSArray<UIViewController *> *)previousViewControllers transitionCompleted:(BOOL)completed
{
    
    _isEnd = YES;
}



#pragma mark - 2.0 实现数据源方法
//    MARK:1.向上翻页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerBeforeViewController:(LXDetailsViewController *)viewController
{
    
    if (_isEnd) {
        
        //1.获取详情页VC的角标
        NSInteger row = viewController.currentIndexPath.row;
        NSInteger section = viewController.currentIndexPath.section;
        
        //3.角标 --
        row--;
        
        //2.判断
        if (row < 0) {
            
            section--;
            if (section < 0) {

                return nil;
            }
    
            row = _foodArray[section].spus.count - 1;
            
        }
        
        //4.生成新的角标
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:row inSection:section];
        
        //5.根据新角标生成 新的详情控制器
        return [self creatDetailsVc:newPath];
    }
    
    return nil;
}

//    MARK:2.向下翻页
- (nullable UIViewController *)pageViewController:(UIPageViewController *)pageViewController viewControllerAfterViewController:(LXDetailsViewController *)viewController
{
    
    if (_isEnd) {
        
        //1.获取当前的详情控制器的角标.
        NSInteger row = viewController.currentIndexPath.row;
        NSInteger section = viewController.currentIndexPath.section;
        
        //2.++
        row++;
        
        //3.判断
        if (row == _foodArray[section].spus.count) {
            
            section++;
            if (section == _foodArray.count) {
    
                return nil;
            }

            row = 0;
        }
        
        //4.新角标
        NSIndexPath *newPath = [NSIndexPath indexPathForRow:row inSection:section];
        
        //5.新的详情控制器
        return [self creatDetailsVc:newPath];
    }
    
    return nil;
}

#pragma mark - 1.0 搭建界面

- (void)setupUI
{
    
    //    MARK: 1.创建PageVc
    UIPageViewController *pageVc = [[UIPageViewController alloc]initWithTransitionStyle:UIPageViewControllerTransitionStylePageCurl navigationOrientation:UIPageViewControllerNavigationOrientationHorizontal options:nil];
    
    //    MARK: 2.设置内容
    [pageVc setViewControllers:@[[self creatDetailsVc:_selectIndexPath]] direction:UIPageViewControllerNavigationDirectionForward animated:NO completion:nil];
    
    //    MARK:3.设置数据源
    pageVc.dataSource = self;
    
    //    MARK:4.设置代理
    pageVc.delegate = self;
    
    //    MARK:4.pageVc添加
    [self cz_addChildController:pageVc intoView:self.view];
    //    MARK:5.添加购物车的View
    LXBuyCarView *buyCarView = [[[NSBundle mainBundle] loadNibNamed:@"LXBuyCarView" owner:nil options:nil] lastObject];
    
    //显示 列表页 已经选中的数据
    buyCarView.buyArrayList = _selectArray;
    
    self.buyCarView = buyCarView;

    [self.view addSubview:buyCarView];
    
    [buyCarView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.bottom.right.equalTo(self.view);
        make.height.mas_equalTo(46);
    }];
}


//    MARK:封装创建详情 控制器
- (UIViewController *)creatDetailsVc:(NSIndexPath *)currentIndexpath
{
    
    LXDetailsViewController *detailsVc = [[LXDetailsViewController alloc]init];
    
    detailsVc.detailsModel = _foodArray[currentIndexpath.section].spus[currentIndexpath.row];
    
    // 赋值当前的角标
    detailsVc.currentIndexPath = currentIndexpath;
    
    return detailsVc;
}



@end
